<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" label-width="auto" :inline="true">
        <el-form-item label="订单类型" prop="orderTypes">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.orderTypes" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in orderTypeArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="平台" prop="platformIds">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.platformIds" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in platFormArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelIds">
          <el-select multiple clearable @keyup.enter="handleQuery" v-model="queryParams.channelIds" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in channelArr" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityId">
          <el-select filterable clearable @keyup.enter="handleQuery" v-model="queryParams.cityId" placeholder="请选择" style="width: 16rem">
            <el-option v-for="item in cityOptions" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="起点名称" prop="fromName">
          <el-input style="width: 16rem" v-model="queryParams.fromName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="终点名称" prop="toName">
          <el-input style="width: 16rem" v-model="queryParams.toName" placeholder="请输入" clearable @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="用车时间">
          <el-date-picker
            style="width: 16rem"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleUseTimeChange"
            v-model="useTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker
            style="width: 16rem"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
            @change="handleCreateTimeChange"
            v-model="createdTimes"
            type="daterange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
          />
        </el-form-item>
        <el-form-item label="有无报价" prop="vehicleEmpty">
          <el-select clearable @keyup.enter="handleQuery" v-model="queryParams.vehicleEmpty" placeholder="请选择" style="width: 16rem">
            <el-option :value="false" label="有报价" />
            <el-option :value="true" label="无报价" />
          </el-select>
        </el-form-item>
        <el-form-item prop="timeDiffFrom" label="距离用车时间">
          <el-input style="width: 114px !important" placeholder="请输入" v-model="queryParams.timeDiffFrom"></el-input>
          <div style="margin: 0 7px">至</div>
          <el-form-item prop="timeDiffTo">
            <el-input style="width: 114px !important" placeholder="请输入" v-model="queryParams.timeDiffTo"></el-input>
          </el-form-item>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button @click="handleExport" type="warning">
          <template #icon><Edit /></template>
          导出
        </el-button>
      </template>
      <el-table v-loading="loading" :data="TableData" highlight-current-row row-key="id" @row-click="handleRowClick">
        <el-table-column label="车型报价" align="center" prop="vehicles">
          <template #default="scope">
            <div v-for="(item, index) in scope.row.vehicles" :key="index">
              <div>￥{{ item.price }}-{{ item.vehicleTypeName }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column min-width="140" label="来源" align="center">
          <template #default="scope">
            <div style="display: flex">
              <div>{{ bizTypeEnum[scope.row.bizType] }}</div>
              <div>-{{ platFormEnum[scope.row.platformId] }}</div>
              <div>-{{ channelEnum[scope.row.channelId] }}</div>
              <div>-{{ orderTypeEnum[scope.row.orderType] }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="路程信息" min-width="200" align="left">
          <template #default="scope">
            <div class="address_box">
              <div class="item">
                <div class="tag">起点</div>
                <div class="content">{{ scope.row.fromCityName }}-{{ scope.row.fromCityArea }}-{{ scope.row.fromName }}</div>
              </div>
              <div class="item">
                <div class="tag tag2">终点</div>
                <div class="content">{{ scope.row.toCityName }}-{{ scope.row.toCityArea }}-{{ scope.row.toName }}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用车城市" align="center" prop="cityName"></el-table-column>
        <el-table-column label="预估里程" align="center" prop="estimateDistance"></el-table-column>
        <el-table-column label="预估时间" align="center" prop="estimateDuration"></el-table-column>
        <el-table-column label="距离用车时间" align="center" prop="timeDiff"></el-table-column>
        <el-table-column label="用车时间" align="center" prop="useTime"></el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime"></el-table-column>
      </el-table>
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </el-card>
    <!-- 改派 -->
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "SysMenu",
  inheritAttrs: false,
});

import OrderApi from "@/api/order";
import ExportApi from "@/api/exports";
import CommonApi from "@/api/common";
import { orderStatusEnum, platFormEnum, channelEnum, orderTypeEnum, bizTypeEnum, luggageEnum, channelArr, bizTypeArr, orderTypeArr, platFormArr, carTypeArr } from "@/enums/common";
import { MenuTypeEnum } from "@/enums/MenuTypeEnum";
import { log } from "console";

const queryFormRef = ref(ElForm);
const total = ref(0);
const cityOptions = ref();
const useTimes = ref();
const createdTimes = ref();

const loading = ref(false);

// 查询参数
const queryParams = reactive<MenuQuery>({
  pageNo: 1,
  pageSize: 10,
});
// 菜单表格数据
const TableData = ref([]);

// 选择表格的行菜单ID
const selectedMenuId = ref<number | undefined>();

// 查询菜单
function handleQuery() {
  loading.value = true;
  OrderApi.statEstimateList(queryParams)
    .then((data) => {
      TableData.value = data.result;
      total.value = data.total * 1;
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleUseTimeChange(time: any) {
  if (time) {
    queryParams.useTimeFrom = `${time[0]} 00:00:00`;
    queryParams.useTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.useTimeFrom = undefined;
    queryParams.useTimeTo = undefined;
  }
}
function handleCreateTimeChange(time: any) {
  if (time) {
    queryParams.createTimeFrom = `${time[0]} 00:00:00`;
    queryParams.createTimeTo = `${time[1]} 23:59:59`;
  } else {
    queryParams.createTimeFrom = undefined;
    queryParams.createTimeFrom = undefined;
  }
}

function handleExport() {
  ExportApi.priceRecordExport(queryParams).then((res) => {
    let data = res.data;
    ElMessage.success("导出完毕");
    if (!data) {
      return;
    }
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([data], { type: "application/vnd.ms-excel" }), fileName + ".xls");
    } else {
      let url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.ms-excel" }));
      let link = document.createElement("a");
      link.style.display = "none";
      link.href = url;
      link.setAttribute("download", "询价记录" + ".xlsx");
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link); //下载完成移除元素
      window.URL.revokeObjectURL(url); //释放掉blob对象
    }
  });
}

// 重置查询
function handleResetQuery() {
  queryParams.useTimeFrom = undefined;
  queryParams.useTimeTo = undefined;
  queryParams.createTimeFrom = undefined;
  queryParams.createTimeTo = undefined;
  useTimes.value = null;
  createdTimes.value = null;

  queryFormRef.value.resetFields();
  queryParams.pageNo = 1;
  handleQuery();
}

// 行点击事件
function handleRowClick(row: MenuVO) {
  // 记录表格选择的菜单ID，新增子菜单作为父菜单ID
  selectedMenuId.value = row.id;
}

onMounted(() => {
  handleQuery();
  CommonApi.getCity({ pageNo: 1, pageSize: 9999 }).then((data) => {
    if (data.code == 0) {
      cityOptions.value = data.result;
    }
  });
});
</script>
<style lang="scss" scoped>
.red_rate {
  color: red;
}
.address_box {
  display: flex;
  flex-direction: column;
  align-items: left;
  justify-content: left;
  .item {
    display: flex;
    align-items: center;
    margin: 4px 0;
    .tag {
      width: 50px;
      height: 23px;
      text-align: center;
      color: orange;
      background: #fff8e6;
    }
    .tag2 {
      color: #3fd175;
      background: #e7faf0;
    }
  }
}
</style>
